<?php
/**
 * Controller_Admin_Selection
 *
 * Класс Controller_Admin_Selection отвечает за обработку админских страниц подборок
 *
 * @author Mike
 */
class Controller_Admin_Selection extends Controller_Admin {

	/**
	 * Выбор действия по URL
	 *
	 */
	public function action() {
		if(!User_Seance::isAdmin()) throw new Exception('', 403);
		switch ($this->arrRequest[2]) {
			case '':
				$this->actionSelectionsList();
				break;
			case 'add':
				$this->actionAdd();
				break;
			case 'edit':
				$objSelection = new Selection(intval($this->arrRequest[3]));
				if(!$objSelection->id) throw new Exception('', 404);
				$this->actionEdit($objSelection);
				break;
			case 'edit_products':
				if(!$this->arrRequest[3])  throw new Exception('', 404);
				
				$objSelection = new Selection(intval($this->arrRequest[3]));
				if(!$objSelection->id) throw new Exception('', 404);
				
				$this->actionEditSelectionProducts($objSelection);
				break;
			default: throw new Exception('', 404);
		}
	}

	/**
	 * Выводит список подборок
	 *
	 */
	public function actionSelectionsList() {
		$arrSelections = Selection::getAllSelections();
	
		$this->bodyAttr = 'selections cms';
		$this->title = 'Подборки';
		$this->pageHead = $this->title . '(<a href="' . W_ADMIN . 'selections/add">Добавить</a>)';
		
		 
		$this->showHeader();
			View_Selection::showAdminList($arrSelections);
		$this->showFooter();
	}
	
	/**
	 * Добавление подборки
	 *
	 */
	public function actionAdd() {
		if (isset($_POST['Selection'])) {
			$objSelection = new Selection($_POST['Selection']);
			if ($objSelection->save()) User_Seance::moveTo(W_ADMIN . 'selections');
		}
		else $objSelection = new Selection();
	
		$this->title = $this->pageHead = 'Добавление подборки';
			
		$this->showHeader();
			View_Selection::showEditForm($objSelection);
		$this->showFooter();
	}
	
	/**
	 * Редактирование подборки
	 *
	 * @param Selection $objSelection - объект подборки
	 */
	public function actionEdit(Selection $objSelection) {
		if (isset($_POST['Selection'])) {
			$objSelection = new Selection($_POST['Selection']);
			if ($objSelection->save()) User_Seance::moveTo(W_ADMIN . 'selections');
		}
	
		$this->title = $this->pageHead = 'Редактирование подборки';
			
		$this->showHeader();
			View_Selection::showEditForm($objSelection);
		$this->showFooter();
	}
	
	/**
	 * Редактирование товаров подборки
	 *
	 * @param Selection $objSelection - объект подборки
	 */
	public function actionEditSelectionProducts(Selection $objSelection) {	
		$arrProducts = $objSelection->getProducts();
		$arrSelectionProductsIds = Service_ArrayConverter::getColumn($arrProducts, 'ID_product');
		$arrAllProducts = Product::getProductsByCategoriesIds($arrSelectionProductsIds);
		
		$this->bodyAttr = 'selections-products cms';
		$this->title = $this->pageHead = 'Товары подборки "' . $objSelection->getField('selection_name') . '"';
			
		$this->showHeader();
			View_Selection::showEditSelectionProductsForm($objSelection, $arrAllProducts, $arrProducts);
		$this->showFooter();
	}
	
	
}